<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="部门" prop="deptName">
            <el-popover placement="bottom" width="300" trigger="focus" v-model="visibleTree">
              <el-input slot="reference" v-model="dataForm.deptName"></el-input>
              <el-tree :data="deptTreeData" ref="tree" node-key="id" default-expand-all :expand-on-click-node="false"
                :props="treeProps" @node-click="handleDeptCheckChange"></el-tree>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库" prop="stockId">
            <el-select v-model="dataForm.stockId" placeholder="请选择" @change="stockChange">
              <el-option v-for="item in stockOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="库存类型" prop="type">
            <el-select v-model="dataForm.type" placeholder="请选择类型">
              <el-option label="隐形出库" value="1"></el-option>
              <el-option label="不合格品入库" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-switch v-model="dataForm.status" inactive-value="0" active-value="1" active-text="启用"></el-switch>
          </el-form-item>
        </el-col>
      </el-row>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getAutoStockConfigObj, addAutoStockConfigObj, putAutoStockConfigObj } from '@/api/eims/autostockconfig'
  import { getStockList } from '@/api/eims/stock'
  import { fetchTree } from '@/api/admin/dept'
  export default {
    data() {
      return {
        visible: false,
        ischeck: false,
        visibleTree: false,
        deptTreeData: [],
        stockOptions: [],
        treeProps: {
          children: 'children',
          label: 'name'
        },
        dataForm: {
          deptId: '',
          deptName: '',
          stockId: '',
          stockName: '',
          type: '',
          status: '1'
        },
        dataRule: {
          deptName: [{
            required: true,
            message: '部门id不能为空',
            trigger: 'blur'
          }],
          stockId: [{
            required: true,
            message: '仓位id不能为空',
            trigger: 'blur'
          }],
          type: [{
            required: true,
            message: '自动调拨类型不能为空',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '状态不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.dataForm.stockId = ''
        this.dataForm.stockName = ''
        this.visible = true
        this.getDeptTree()
        this.getAutoStockList()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getAutoStockConfigObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 部门树change事件
      handleDeptCheckChange(data, checked, indeterminate) {
        this.dataForm.deptId = data.id
        this.dataForm.deptName = data.name
        this.visibleTree = false
        this.getAutoStockList(data.id);
      },
      // 获取部门树
      getDeptTree() {
        this.deptTreeData = []
        fetchTree().then(response => {
          if(response.data.code == '0'){
            this.deptTreeData = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 获取库仓位列表
      getAutoStockList(id) {
        this.dataForm.stockId = ''
        this.stockOptions = [];
        getStockList().then(response => {
          if(response.data.code == '0'){
            let resultData = response.data.data
            let stockList = []
            resultData.forEach(item => {
              let stockObj = {
                value: item.id,
                label: item.name
              }
              stockList.push(stockObj)
            })
            this.stockOptions = stockList
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      stockChange(data) {
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putAutoStockConfigObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = true
              })
            } else {
              addAutoStockConfigObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = true
              })
            }
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
